package com.dtu.ImagesExtras;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dtu.ImagesExtras.AsyncImageLoader.ImageCallback;
import com.dtu.snapdroid.R;


public class ImageGridAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    
    private ArrayList<ImageAndText> myImageAndTexts;
    private AsyncImageLoader asyncImageLoader;
    private GridView g;
    private Activity activity;
    

    public ImageGridAdapter(Context c, Activity activity, ArrayList<ImageAndText> imageAndTexts, GridView g) {
        mContext = c;
        myImageAndTexts = imageAndTexts;
        asyncImageLoader = new AsyncImageLoader();
        this.g = g;
        this.activity = activity;
        
        TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();

    }

    public int getCount() {
    	try {
    		return myImageAndTexts.size();

    	} catch (Exception e) {
    		return 0;
    	}
    }

    public Object getItem(int position) {
        return this.myImageAndTexts.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        
    	// Inflate the views from XML
        View rowView = convertView;
        ViewCache viewCache;
        if (rowView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.image_and_text_row, null);
            viewCache = new ViewCache(rowView);
            rowView.setTag(viewCache);
        } else {
            viewCache = (ViewCache) rowView.getTag();
        }
        ImageAndText imageAndText = (ImageAndText) getItem(position);

        // Load the image and set it on the ImageView
        String imageUrl = "http://www.loftur.com/Pics/Thumb/" + imageAndText.getImageUrl() + ".jpg";
        ImageView imageView = viewCache.getImageView();
        imageView.setTag(imageUrl);
        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                ImageView imageViewByTag = (ImageView) g.findViewWithTag(imageUrl);

                if (imageViewByTag != null) {
                    imageViewByTag.setImageDrawable(imageDrawable);
                }
            }
        });
        imageView.setImageDrawable(cachedImage);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(2, 2, 2, 2);

        /*
        // Set the text on the TextView
        TextView textView = viewCache.getTextView();
        textView.setText(imageAndText.getText());
 		*/
        return rowView;
    }
}